<template>
  <a-card :loading="cardLoading" :bordered="false" style="height: 100%;">
    <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="规则编码">
        <a-input v-model="form.code" />
      </a-form-model-item>
      <a-form-model-item label="规则名称">
        <a-input v-model="form.name" />
      </a-form-model-item>
      <a-form-model-item label="规则描述">
        <a-textarea
          v-model="form.describe"
          placeholder="请输入规则描述"
          :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-model-item>
      <a-form-model-item label="校验类型" >
        <a-select v-model="form.region" placeholder="请选择" @change="handleChange">
          <a-select-option value="sql">
            sql脚本
          </a-select-option>
          <a-select-option value="java">
            java类
          </a-select-option>
        </a-select>

        <a-textarea v-if="this.form.region == 'java'"
                    v-model="form.javaClass"
                    placeholder="请输入java类路径"
                    :auto-size="{ minRows: 3, maxRows: 5 }"
        />
        <a-textarea v-if="this.form.region == 'sql'"
                    v-model="form.sqlScript"
                    placeholder="请输入sql脚本"
                    :auto-size="{ minRows: 3, maxRows: 5 }"
        />
      </a-form-model-item>


    </a-form-model>
    <div class="anty-form-btn"  style="padding-top: 150px">
      <a-button @click="emptyCurrForm" type="default" htmlType="button" icon="sync">重置</a-button>
      <a-button @click="submitCurrForm" type="primary" htmlType="button" icon="form">保存</a-button>
    </div>
  </a-card>
</template>
<script>
  import { httpAction } from '@/api/manage'

  const plainOptions = [{ label: '不为null', value: 'notNull' },{ label: '不为空字符串', value: 'notNullString' }];
  var llbbReportRule ={
    ruleCode: '',
    ruleName: '',
    ruleClass: '',
    ruleLevel: '',
    ruleDesc: '',
    ruleSql: '',
    ruleParam: '',
    reportName: '',
    reportField: '',
    reportRuleStatus: '',
  }

  export default {
    name: 'NonNullPage',
    props: {
      'tableName': String,
      'columnName': String,
      'ruleData': Number,
      'title': String,
    },
    data() {
      return {
        switchChecked: true,
        sqlValue: '',
        checkedList: [],
        indeterminate: true,
        checkAll: false,
        plainOptions,
        cardLoading: false,
        loading: false,
        labelCol: { span: 4 },
        wrapperCol: { span: 14 },
        form: {
          name: '',
          region: undefined,
          date1: undefined,
          delivery: false,
          type: [],
          resource: '',
          desc: '',
        },
      };
    },
    methods: {
      handleChange(value) {
        console.log(`selected ${value}`);
      },
      onChange(checkedList) {
        this.indeterminate = !!checkedList.length && checkedList.length < plainOptions.length;
        this.checkAll = checkedList.length === plainOptions.length;
      },
      switchonChange(checked) {
        this.switchChecked = !checked
        if(!checked){
          this.sqlValue = ''
        }

       /* console.log(`a-switch to ${checked}`);*/
      },
      onCheckAllChange(e) {
        Object.assign(this, {
          checkedList: e.target.checked ? plainOptions : [],
          indeterminate: false,
          checkAll: e.target.checked,
        });
      },
      emptyCurrForm(){
        this.form.region = undefined
        this.checkedList = []

      },
      submitCurrForm(){
        var tableColumn = '';
        var name = '';
        if(this.checkedList.length == 0){
          this.$message.warn("请勾选选项！")
        }else{
          var  that = this
          llbbReportRule.ruleCode = 'IRR-T001'
          llbbReportRule.ruleName = '非空校验'
          llbbReportRule.ruleClass = '1'
          llbbReportRule.ruleLevel = '1'
          llbbReportRule.ruleSql = this.sqlValue
          llbbReportRule.reportName = this.tableName
          llbbReportRule.reportField = this.columnName
          llbbReportRule.reportRuleStatus ='1'

          this.checkedList.forEach(function(element) {
            that.plainOptions.forEach(function(ele) {
              if(element == ele.value){
                name =name+that.title + ele.label +","
              }
            });
            tableColumn = tableColumn +element+","
          });
          llbbReportRule.ruleDesc = name.substring(0, name.lastIndexOf(','));
          llbbReportRule.ruleParam = tableColumn.substring(0, tableColumn.lastIndexOf(','));

          httpAction("/reportrule/llbbReportRule/add",llbbReportRule,'post').then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
              this.$emit('renovateTbleList', true)

            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            this.checkedList = []
            this.sqlValue = ''
          })
        }
      }
    },
  };
</script>
<style scoped>
  @import '~@assets/less/common.less';
  .ant-card-body .table-operator {
    margin: 15px;
  }

  .anty-form-btn {
    width: 100%;
    text-align: center;
  }

  .anty-form-btn button {
    margin: 0 5px;
  }

  .anty-node-layout .ant-layout-header {
    padding-right: 0
  }

  .header {
    padding: 0 8px;
  }

  .header button {
    margin: 0 3px
  }

  .ant-modal-cust-warp {
    height: 100%
  }

  .ant-modal-cust-warp .ant-modal-body {
    height: calc(100% - 110px) !important;
    overflow-y: auto
  }

  .ant-modal-cust-warp .ant-modal-content {
    height: 90% !important;
    overflow-y: hidden
  }

  #app .desktop {
    height: auto !important;
  }

  /** Button按钮间距 */
  .ant-btn {
    margin-left: 3px
  }

  .drawer-bootom-button {
    /*position: absolute;*/
    bottom: 0;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: left;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
</style>